package com.penghaisoft.microwms.base.web.data.controller;

import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.base.web.data.service.IMdSupplierService;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdSupplier;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;

/**
 * @Description 供应商
 * @author luoteng
 * @date 2018年7月24日 下午9:52:32
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/supplier")
@Slf4j
public class MdSupplierController extends BaseController {

	@Autowired
	private IMdSupplierService mdSupplierService;

	/**
	 * 新增记录
	 * 
	 * @param mdSupplier
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(MdSupplier mdSupplier) {
		mdSupplier.setActiveFlag("1");
		mdSupplier.setCreateBy(getLoginName());
		mdSupplier.setGmtCreate(new Date());
		Resp resp = mdSupplierService.create(mdSupplier);
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * 
	 * @param mdSupplier
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(MdSupplier mdSupplier) {
		mdSupplier.setLastModifiedBy(getLoginName());
		mdSupplier.setGmtModified(new Date());
		Resp resp = mdSupplierService.delete(mdSupplier);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			MdSupplier mdSupplier) {
		Pager<MdSupplier> result = mdSupplierService.findListByCondition(page,
				rows, mdSupplier);
		return success(result);
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		MdSupplier mdSupplier = mdSupplierService.findById(id);
		return success(mdSupplier);
	}

	/**
	 * 修改记录
	 * 
	 * @param mdSupplier
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(MdSupplier mdSupplier) {
		mdSupplier.setLastModifiedBy(getLoginName());
		mdSupplier.setGmtModified(new Date());
		Resp resp = mdSupplierService.update(mdSupplier);
		return fromResp(resp);
	}
	
	/** 
	 * @Title query
	 * @Description 获取供应商列表
	 * @author luoteng
	 * @date 2018年7月24日:下午11:22:00
	 * @param mdSupplier
	 * @return TransResult
	 */
	@GetMapping("get")
	public TransResult query(MdSupplier mdSupplier) {
		mdSupplier.setActiveFlag("1");
		List<MdSupplier> list = mdSupplierService.queryList(mdSupplier);
		return success(list);
	}

}
